﻿using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;

namespace jq9602_jq9603.Process
{
    internal partial class TestItem : ObservableObject
    {
        public static int StationCount = 4;
        [ObservableProperty] private string _desc = "";
        [ObservableProperty] private ObservableCollection<double> _stationValue;

        [ObservableProperty] private double _minLimit;
        [ObservableProperty] private double _maxLimit;
        [ObservableProperty] private string _unit = "";
        [ObservableProperty] private ObservableCollection<bool?> _stationIsPassed;

        public string DisplayMinLimit => $"{MinLimit}{Unit}";
        public string DisplayMaxLimit => $"{MaxLimit}{Unit}";

        public string S1DisplayValue => $"{StationValue[0]:F3}{Unit}";
        public string S2DisplayValue => $"{StationValue[1]:F3}{Unit}";
        public string S3DisplayValue => $"{StationValue[2]:F3}{Unit}";
        public string S4DisplayValue => $"{StationValue[3]:F3}{Unit}";

        //public void SetStationValue(int index, double value)
        //{
        //    StationValue[index] = value;
        //    OnPropertyChanged(nameof(StationValue));
        //    OnPropertyChanged($"S{index + 1}DisplayValue");
        //}

        //public void SetResult()
        //{
        //    for (int i = 0; i < StationCount; i++)
        //    {
        //        StationIsPassed[i] = StationValue[i] <= MaxLimit && StationValue[i] >= MinLimit;
        //    }
        //}

        //public void SetResult(int index, bool result)
        //{
        //    StationIsPassed[index] = result;
        //}

        public void SetResult(int index, double value)
        {
            StationValue[index] = value;
            if (MaxLimit == -1)
            {
                if (MinLimit == -1)
                {
                    StationIsPassed[index] = true;
                }
                else
                {
                    StationIsPassed[index] = StationValue[index] >= MinLimit;
                }
            }
            else
            {
                if (MinLimit == -1)
                {
                    StationIsPassed[index] = StationValue[index] <= MaxLimit;
                }
                else
                {
                    StationIsPassed[index] = StationValue[index] <= MaxLimit && StationValue[index] >= MinLimit;
                }
            }

            //StationIsPassed[index] = StationValue[index] <= MaxLimit && StationValue[index] >= MinLimit;
            OnPropertyChanged(nameof(StationValue));
            OnPropertyChanged($"S{index + 1}DisplayValue");
        }

        public TestItem()
        {
            StationValue = new ObservableCollection<double>();
            StationIsPassed = new ObservableCollection<bool?>();
            for (int i = 0; i < StationCount; i++)
            {
                StationValue.Add(0);
                StationIsPassed.Add(null);
            }
        }
    }
}